"use strict";
cc._RF.push(module, '92352MwYtFGIbUXK2ZuoyI1', 'game');
// Scripts/game.js

'use strict';

cc.Class({
  extends: cc.Component,
  properties: {
    cow_prefab: {
      type: cc.Prefab,
      default: null
    },
    audio: {
      type: cc.AudioClip,
      default: null
    }
  },
  onLoad: function onLoad() {
    this.time_src = cc.find('Home/time/src').getComponent(cc.Label);
    this.count_src = cc.find('Home/count/src').getComponent(cc.Label);
    this.checkout = cc.find('Home/checkout');
    this.score = cc.find('Home/checkout/board/score').getComponent(cc.Label);
    this.title = cc.find('Home/checkout/title').getComponent(cc.Label);
    this.startGame();
  },
  startGame: function startGame() {
    this.checkout.active = false;
    // 初始化数据
    this.cows = [];
    this.time_num = 60;
    this.time_src.string = '' + this.time_num;
    this.count_num = 0;
    this.count_src.string = '' + this.count_num;
    // 倒计时
    this.schedule(this.countDown, 1);
    this.schedule(this.produceCow, Math.random() * 3 + 3); // // [3, 6)秒生产一头牛
  },
  countDown: function countDown() {
    this.time_num--;
    this.time_src.string = '' + this.time_num;
    if (this.time_num <= 0) {
      // 时间到
      this.unscheduleAllCallbacks(); // 停止所有计时器
      // 进入结算
      this.toCheckout();
      return;
    }
  },
  produceCow: function produceCow() {
    var cow = cc.instantiate(this.cow_prefab); // 实例化(复制)一头牛
    this.node.addChild(cow);
    this.cows.push(cow);
    cow.x = 500;
    cow.y = -140;
  },
  catchCow: function catchCow() {
    var type_index = -1;
    for (var i = 0; i < this.cows.length; i++) {
      if (this.cows[i].x > 90 && this.cows[i].x < 130) {
        // 套到了牛
        type_index = this.cows[i].getComponent('cow').cow_type;
        // 删除该牛
        this.cows[i].removeFromParent();
        this.cows.splice(i, 1);
        // 计数+1
        this.count_num++;
        this.count_src.string = '' + this.count_num;
        // 音效
        cc.audioEngine.play(this.audio);
        return type_index;
      }
    }
    return -1;
  },
  toCheckout: function toCheckout() {
    this.checkout.active = true;
    this.score.string = '' + this.count_num;
    var total = this.count_num;
    if (total <= 3) {
      this.title.string = '情场空手';
    } else if (total <= 6) {
      this.title.string = '情场新手';
    } else if (total <= 8) {
      this.title.string = '情场高手';
    } else {
      this.title.string = '情场圣手';
    }
  },
  start: function start() {
    // 设置层级
    this.checkout.zIndex = 3000;
    var button = this.node.getChildByName('action_btn');
    button.zIndex = 2000;
    var rope = this.node.getChildByName('rope');
    rope.zIndex = 1000;
  }
});

cc._RF.pop();